package com.ruoyi.business.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.business.domain.Enterprise;
import com.ruoyi.business.domain.IntegralDetail;
import com.ruoyi.business.domain.ViolationCategory;
import com.ruoyi.business.dto.AddApplicationDTO;
import com.ruoyi.business.dto.AuditDTO;
import com.ruoyi.business.mapper.EnterpriseMapper;
import com.ruoyi.business.mapper.IntegralDetailMapper;
import com.ruoyi.business.mapper.ViolationCategoryMapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.ApplicationMapper;
import com.ruoyi.business.domain.Application;
import com.ruoyi.business.service.IApplicationService;
import org.springframework.util.ObjectUtils;

/**
 * 申请Service业务层处理
 * 
 * @author lcz
 * @date 2025-01-19
 */
@Service
public class ApplicationServiceImpl implements IApplicationService 
{
    @Autowired
    private ApplicationMapper applicationMapper;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private ViolationCategoryMapper violationCategoryMapper;

    @Autowired
    private EnterpriseMapper enterpriseMapper;

    @Autowired
    private IntegralDetailMapper integralDetailMapper;

    /**
     * 查询申请
     * 
     * @param id 申请主键
     * @return 申请
     */
    @Override
    public Application selectApplicationById(Long id)
    {
        return applicationMapper.selectApplicationById(id);
    }

    /**
     * 查询申请列表
     * 
     * @param application 申请
     * @return 申请
     */
    @Override
    public List<Application> selectApplicationList(Application application)
    {
        return applicationMapper.selectApplicationList(application);
    }

    /**
     * 新增申请
     * 
     * @param application 申请
     * @return 结果
     */
    @Override
    public int insertApplication(Application application)
    {
        application.setCreateTime(DateUtils.getNowDate());
        return applicationMapper.insertApplication(application);
    }

    /**
     * 修改申请
     * 
     * @param application 申请
     * @return 结果
     */
    @Override
    public int updateApplication(Application application)
    {
        int res = applicationMapper.updateApplication(application);

        //若审核不通过，则加回积分
        if (application.getAuditStatus().equals("2")){
            Enterprise enterprise = enterpriseMapper.selectEnterpriseById(application.getEnterpriseId());
            enterprise.setIntegral(enterprise.getIntegral()+application.getIntegral());
            enterprise.setIsViolation("1");
            enterpriseMapper.updateEnterprise(enterprise);

            IntegralDetail integralDetail = new IntegralDetail();
            integralDetail.setEnterpriseId(application.getEnterpriseId());
            integralDetail.setIntegral(application.getIntegral());
            integralDetail.setSource("违规类目《"+application.getCategoryName()+"》的抵消违规申请审核未通过");
            integralDetail.setCreateTime(new Date());
            integralDetailMapper.insertIntegralDetail(integralDetail);
        }
        return res;
    }

    /**
     * 批量删除申请
     * 
     * @param ids 需要删除的申请主键
     * @return 结果
     */
    @Override
    public int deleteApplicationByIds(Long[] ids)
    {
        return applicationMapper.deleteApplicationByIds(ids);
    }

    /**
     * 删除申请信息
     * 
     * @param id 申请主键
     * @return 结果
     */
    @Override
    public int deleteApplicationById(Long id)
    {
        return applicationMapper.deleteApplicationById(id);
    }

    @Override
    public AjaxResult addApplication(AddApplicationDTO dto) {
        ViolationCategory violationCategory = violationCategoryMapper.selectViolationCategoryById(dto.getViolationCategoryId());
        Long id = tokenService.getUserId();
        Enterprise enterprise = enterpriseMapper.selectEnterpriseById(id);
        if (enterprise.getIntegral()<violationCategory.getIntegral()){
            return AjaxResult.error("当前积分不足");
        }
        Application application = new Application();
        application.setViolationCategoryId(violationCategory.getId());
        application.setCategoryName(violationCategory.getName());
        application.setIntegral(violationCategory.getIntegral());
        application.setEnterpriseId(enterprise.getId());
        application.setEnterpriseName(enterprise.getEnterpriseName());
        application.setAuditStatus("0");
        application.setContent(dto.getContent());
        application.setCreateTime(new Date());
        applicationMapper.insertApplication(application);

        //扣除积分
        enterprise.setIntegral(enterprise.getIntegral()-application.getIntegral());
        enterprise.setIsViolation("2");
        enterpriseMapper.updateEnterprise(enterprise);

        IntegralDetail integralDetail = new IntegralDetail();
        integralDetail.setEnterpriseId(application.getEnterpriseId());
        integralDetail.setIntegral(-application.getIntegral());
        integralDetail.setSource("对违规类目《"+application.getCategoryName()+"》提交抵消违规申请单");
        integralDetail.setCreateTime(new Date());
        integralDetailMapper.insertIntegralDetail(integralDetail);
        return AjaxResult.success("已提交申请，请耐心等待审核");
    }

    @Override
    public AjaxResult audit(AuditDTO dto) {
        Application application = applicationMapper.selectApplicationById(dto.getId());
        if (ObjectUtils.isEmpty(application)){
            return AjaxResult.error("系统中不存在该申请单！");
        }
        application.setAuditStatus(dto.getAuditStatus());
        applicationMapper.updateApplication(application);

        //若审核不通过，则加回积分
        if (dto.getAuditStatus().equals("2")){
            Enterprise enterprise = enterpriseMapper.selectEnterpriseById(application.getEnterpriseId());
            enterprise.setIntegral(enterprise.getIntegral()+application.getIntegral());
            enterprise.setIsViolation("1");
            enterpriseMapper.updateEnterprise(enterprise);

            IntegralDetail integralDetail = new IntegralDetail();
            integralDetail.setEnterpriseId(application.getEnterpriseId());
            integralDetail.setIntegral(application.getIntegral());
            integralDetail.setSource("违规类目《"+application.getCategoryName()+"》的抵消违规申请审核未通过");
            integralDetail.setCreateTime(new Date());
            integralDetailMapper.insertIntegralDetail(integralDetail);
        }
        return AjaxResult.success();
    }
}
